package com.xy.service.mainData;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xy.api.model.dto.mainData.VendorDTO;
import com.xy.api.model.dto.mainData.VendorProductTypeDTO;
import com.xy.api.model.request.mainData.VendorProductTypeRequest;
import com.xy.api.model.response.Rows;
import com.xy.api.service.mainData.IVendorProductTypeService;
import com.xy.auth.UserContext;
import com.xy.mapper.mainData.VendorProductTypeMapper;
import com.xy.model.po.mainData.VendorPO;
import com.xy.model.po.mainData.VendorProductTypePO;
import com.xy.utils.CustomBeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class VendorProductTypeServiceImpl implements IVendorProductTypeService {

    @Autowired
    private VendorProductTypeMapper vendorProductTypeMapper;

    @Override
    public Rows<VendorProductTypeDTO> queryVendorProductTypeList(VendorProductTypeRequest request) {
        Assert.notNull(request.getVendorId(), "供应商ID必填");
        Rows<VendorProductTypeDTO> result = new Rows<>();
        LambdaQueryWrapper<VendorProductTypePO> vendorProductTypeWrapper = new LambdaQueryWrapper<>();
        vendorProductTypeWrapper
                .eq(VendorProductTypePO::getIsDeleted, 0)
                .eq(VendorProductTypePO::getVendorId, request.getVendorId());

        int pageSize = null != request.getPageSize() ? request.getPageSize() : 10;
        int pageNum = null != request.getPageNum() ? request.getPageNum() : 1;
        Page<VendorProductTypePO> vendorProductTypePOPage = new Page<>(pageNum, pageSize);
        vendorProductTypePOPage = vendorProductTypeMapper.selectPage(vendorProductTypePOPage, vendorProductTypeWrapper);

        result.setTotal(vendorProductTypePOPage.getTotal());
        result.setRows(CustomBeanUtil.copyListProperties(vendorProductTypePOPage.getRecords(), VendorProductTypeDTO::new));
        return result;
    }

    @Override
    public Rows<VendorProductTypeDTO> queryAllVendorProductTypeList(VendorProductTypeRequest request) {
        Rows<VendorProductTypeDTO> result = new Rows<>();
        LambdaQueryWrapper<VendorProductTypePO> vendorProductTypeWrapper = new LambdaQueryWrapper<>();
        vendorProductTypeWrapper
                .eq(VendorProductTypePO::getIsDeleted, 0)
                .eq(VendorProductTypePO::getVendorId, request.getVendorId());
        List<VendorProductTypePO> vendorProductTypePOS = vendorProductTypeMapper.selectList(vendorProductTypeWrapper);
        if (CollectionUtils.isEmpty(vendorProductTypePOS)) {
            return new Rows<>(new ArrayList<>(), 0);
        }
        result.setTotal(vendorProductTypePOS.size());
        result.setRows(CustomBeanUtil.copyListProperties(vendorProductTypePOS, VendorProductTypeDTO::new));
        return result;
    }

    @Override
    public VendorProductTypeDTO vendorProductTypeDetail(Long id) {
        Assert.notNull(id, "供应商产品类型ID必填");
        VendorProductTypeDTO vendorProductType = new VendorProductTypeDTO();
        VendorProductTypePO record = vendorProductTypeMapper.selectById(id);
        CustomBeanUtil.copyProperties(record, vendorProductType);
        return vendorProductType;
    }

    @Override
    public void editVendorProductType(VendorProductTypeDTO request) {
        VendorProductTypePO record = new VendorProductTypePO();
        CustomBeanUtil.copyProperties(request, record);
        if (null == record.getId()) {
            addVendorProductType(record);
        } else {
            updateVendorProductType(record);
        }
    }

    private void addVendorProductType(VendorProductTypePO record) {
        record.setCreateBy(UserContext.get().getId());
        vendorProductTypeMapper.insert(record);
    }

    private void updateVendorProductType(VendorProductTypePO record) {
        Assert.notNull(record.getId(), "供应商产品类型ID必填");
        record.setUpdateBy(UserContext.get().getId());
        vendorProductTypeMapper.updateById(record);
    }

    @Override
    public void deleteVendorProductType(Long id) {
        Assert.notNull(id, "供应商产品类型ID必填");

        VendorProductTypePO deleteRecord = new VendorProductTypePO();
        deleteRecord.setId(id);
        deleteRecord.setUpdateBy(UserContext.get().getId());
        deleteRecord.setIsDeleted(1);
        vendorProductTypeMapper.updateById(deleteRecord);
    }

    @Override
    public Map<Long, VendorProductTypeDTO> getVendorProductTypeByIds(List<Long> vendorProductTypeIds) {
        LambdaQueryWrapper<VendorProductTypePO> vendorProductTypeWrapper = new LambdaQueryWrapper<>();
        vendorProductTypeWrapper
                .eq(VendorProductTypePO::getIsDeleted, 0)
                .in(VendorProductTypePO::getId, vendorProductTypeIds);
        List<VendorProductTypePO> vendorProductTypePOS = vendorProductTypeMapper.selectList(vendorProductTypeWrapper);
        if (CollectionUtils.isEmpty(vendorProductTypePOS)) {
            return new HashMap<>();
        }
        List<VendorProductTypeDTO> vendorProductTypeList = CustomBeanUtil.copyListProperties(vendorProductTypePOS, VendorProductTypeDTO::new);
        return vendorProductTypeList.stream().collect(Collectors.toMap(e -> e.getId(), e -> e));
    }
}
